package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.DsClassStu;
import com.ruoyi.system.domain.DsStudent;
import com.ruoyi.system.mapper.DsClassStuMapper;
import com.ruoyi.system.mapper.DsStudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DsClassMapper;
import com.ruoyi.system.domain.DsClass;
import com.ruoyi.system.service.IDsClassService;

/**
 * 课程记录Service业务层处理
 *
 * @author tyro
 * @date 2025-03-23
 */
@Service
public class DsClassServiceImpl implements IDsClassService
{
    @Autowired
    private DsClassMapper dsClassMapper;

    /**
     * 查询课程记录
     *
     * @param classId 课程记录主键
     * @return 课程记录
     */
    @Override
    public DsClass selectDsClassByClassId(Long classId)
    {
        return dsClassMapper.selectDsClassByClassId(classId);
    }

    /**
     * 查询课程记录列表
     *
     * @param dsClass 课程记录
     * @return 课程记录
     */
    @Override
    public List<DsClass> selectDsClassList(DsClass dsClass)
    {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();

        // 如果不是管理员，则添加学院过滤条件
        if(!SecurityUtils.isAdmin(userId)){ // 或使用 SecurityUtils.isAdmin(loginUser.getUser().getUserId())
            dsClass.setCollegeId(SecurityUtils.getLoginUser().getCollegeId());
        }
        return dsClassMapper.selectDsClassList(dsClass);
    }

    /**
     * 新增课程记录
     *
     * @param dsClass 课程记录
     * @return 结果
     */
    @Override
    public int insertDsClass(DsClass dsClass)
    {
        // 设置学院ID
        if (dsClass.getCollegeId() == null) {
            dsClass.setCollegeId(SecurityUtils.getLoginUser().getCollegeId());
        }
        dsClass.setCreateTime(DateUtils.getNowDate());
        return dsClassMapper.insertDsClass(dsClass);
    }

    /**
     * 修改课程记录
     *
     * @param dsClass 课程记录
     * @return 结果
     */
    @Override
    public int updateDsClass(DsClass dsClass)
    {
        return dsClassMapper.updateDsClass(dsClass);
    }

    /**
     * 批量删除课程记录
     *
     * @param classIds 需要删除的课程记录主键
     * @return 结果
     */
    @Override
    public int deleteDsClassByClassIds(Long[] classIds)
    {
        return dsClassMapper.deleteDsClassByClassIds(classIds);
    }

    /**
     * 删除课程记录信息
     *
     * @param classId 课程记录主键
     * @return 结果
     */
    @Override
    public int deleteDsClassByClassId(Long classId)
    {
        return dsClassMapper.deleteDsClassByClassId(classId);
    }

    @Autowired
    private DsClassStuMapper dsClassStuMapper;

    @Autowired
    private DsStudentMapper dsStudentMapper;

    @Override
    public List<DsStudent> selectStudentsByClassId(Long classId) {
        return dsStudentMapper.selectStudentsByClassId(classId);
    }

    @Override
    public int addStudentsToClass(Long classId, List<String> studentIds) {
        int rows = 0;
        // 设置学院ID
        int collegeId = SecurityUtils.getLoginUser().getCollegeId();
        // 检查课程是否存在
        DsClass dsClass = selectDsClassByClassId(classId);
        if (dsClass == null) {
            throw new ServiceException("课程不存在");
        }

        // 批量添加学生到课程
        for (String studentId : studentIds) {
            // 检查学生是否已在课程中
            if (dsClassStuMapper.checkStudentInClass(classId, studentId) == 0) {
                DsClassStu classStu = new DsClassStu();
                classStu.setClassId(classId);
                classStu.setStudentId(Long.valueOf(studentId));
                classStu.setCollegeId(collegeId);
                rows += dsClassStuMapper.insertDsClassStu(classStu);
            }
        }

        return rows;
    }

    @Override
    public int removeStudentsFromClass(Long classId, List<String> studentIds) {
        int rows = 0;

        // 批量从课程移除学生
        for (String studentId : studentIds) {
            rows += dsClassStuMapper.deleteClassStudent(classId, studentId);
        }

        return rows;
    }

    @Override
    public int addStudentsByClassName(Long classId, String className) {
        // 检查课程是否存在
        DsClass dsClass = selectDsClassByClassId(classId);
        if (dsClass == null) {
            throw new ServiceException("课程不存在");
        }

        // 查询指定班级的所有学生
        List<DsStudent> students = dsStudentMapper.selectStudentsByClassName(className);
        if (students == null || students.isEmpty()) {
            return 0;
        }

        int count = 0;
        // 批量添加学生到课程
        for (DsStudent student : students) {
            // 检查学生是否已在课程中
            if (dsClassStuMapper.checkStudentInClass(classId, student.getStudentId()) == 0) {
                DsClassStu classStu = new DsClassStu();
                classStu.setClassId(classId);
                classStu.setStudentId(Long.valueOf(student.getStudentId()));
                count += dsClassStuMapper.insertDsClassStu(classStu);
            }
        }

        return count;
    }
}
